package dao;

import javax.persistence.EntityExistsException;
import javax.persistence.TransactionRequiredException;

import excepciones.DaoException;
import modelo.Criterio;
import modelo.Model;

public class CriterioDao extends AbstractDao {
	
	
	/**
	* Class Constructor [Empty]
	*/
	public CriterioDao()
	{
	   super();
	}
	
	/**
    * Class Constructor
    * @param model   The Usuario model to manage persistence for
    */
    public CriterioDao(Criterio CriterioModel)
    {
      super(CriterioModel);      
      this.model = (Criterio) model;
    }

	@Override
	public Model findById(Long id) throws DaoException {
		// TODO Auto-generated method stub
		try
	      {
	         model = (Criterio) super.findById(Criterio.class, id);
	      }
	      catch(Exception e)
	      {
	         e.printStackTrace();
	      }
	      
	      
	      return (Criterio) model;
	}

	@Override
	public Model getModel() {
		// TODO Auto-generated method stub
		return (Criterio) model;
	}
	
	public void createAccess() throws DaoException
	   {
	      startTxn();
	      try
	      {
	         entityManager.persist( model );
	         entityManager.getTransaction().commit();
	      }
	      catch(EntityExistsException e)
	      {
	         entityManager.getTransaction().rollback();
	         e.printStackTrace();
	         throw new DaoException("Create Failed: " + e.getMessage());
	      }
	      catch(IllegalArgumentException e)
	      {
	         entityManager.getTransaction().rollback();
	         e.printStackTrace();
	         throw new DaoException("Create Failed: " + e.getMessage());
	      }
	      catch(TransactionRequiredException e)
	      {
	         entityManager.getTransaction().rollback();
	         e.printStackTrace();
	         throw new DaoException("Create Failed: " + e.getMessage());
	      }
	   }

}
